﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Liju.Model;
using LiJu.Db;

namespace LiJu
{
    public partial class NGJudgment : Form
    {
        /// <summary>
        /// 异常作业任务实体
        /// </summary>
        public ProductTaskRecord productTaskRecordNG;

        /// <summary>
        /// true 工作模式 false 测试模式
        /// </summary>
        public bool bIsWorkModel = true;

        /// <summary>
        /// 异常判定结果 4品质判定PASS 5品质判定NG 6品质判定重做
        /// </summary>
        public int nCheckState = 0;

        /// <summary>
        /// 异常判定 重做时 是否再次送钉 -1未经异常判定 0判定非重做 1需再次送钉 2不再次送钉
        /// </summary>
        public int nReDoSongDing = -1;

        public NGJudgment()
        {
            InitializeComponent();
        }

        private void NGJudgment_Load(object sender, EventArgs e)
        {
            tbGongHao.Text = tbPassWord.Text = tbReMark.Text = "";
            rbtPass.Checked = rbtNG.Checked = rbtReDo.Checked = false;
            cbNGType.SelectedIndex = 0;
        }

        private void CheckResult(object sender, EventArgs e)
        {
            if (rbtPass.Checked)
            {
                panel1.Enabled = false;
                cbNGType.Enabled = false;
            }
            else if (rbtNG.Checked)
            {
                panel1.Enabled = false;
                cbNGType.Enabled = true;
            }
            else if (rbtReDo.Checked)//重做
            {
                panel1.Enabled = true;
                cbNGType.Enabled = true;
            }
        }

        private void BtTiJiao_Click(object sender, EventArgs e)
        {
            nReDoSongDing = 0;
            if (productTaskRecordNG != null)
            {
                if (tbGongHao.Text.Trim().Length < 1)
                {
                    tbGongHao.Focus();
                    MessageBox.Show("用户名不能为空。");
                    return;
                }
                if (tbPassWord.Text.Trim().Length < 1)
                {
                    tbPassWord.Focus();
                    MessageBox.Show("密码不能为空。");
                    return;
                }

                string sql = string.Format(@"SELECT * from usermanager WHERE (UserID='{0}' or UserName='{0}') and `Password`='{1}'", tbGongHao.Text.Trim(), EncryptionHelper.Encrypt(tbPassWord.Text.Trim()));
                var result = Db.DalMySQL.Query(sql);
                if (result.Tables[0].Rows.Count > 0)
                {
                    //有无NG判断权限
                    var menuRole = PageDataDto.SystemMenuRoleInfo(tbGongHao.Text.Trim());
                    if (menuRole != null && !menuRole.Work_NG)
                    {
                        MessageBox.Show("抱歉，您没有异常判定权限，请联系管理员设置权限！", "无操作权限", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    productTaskRecordNG.NGJudgeUserName = result.Tables[0].Rows[0][2].ToString();
                }
                else
                {
                    MessageBox.Show("请重新输入账号和密码！", "账号或密码输入错误 ！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbGongHao.Text = tbPassWord.Text = "";
                    tbGongHao.Focus();
                    return;
                }
                productTaskRecordNG.NGJudgeGongHao = tbGongHao.Text.Trim();
                productTaskRecordNG.NGType = cbNGType.Text.Trim() == "若NG请选择" ? "" : cbNGType.Text.Trim();
                productTaskRecordNG.NGReMark = tbReMark.Text.Trim();

                if (rbtPass.Checked)//4品质判定PASS  
                {
                    productTaskRecordNG.State = 4;
                }
                else if (rbtNG.Checked)//5品质判定NG 
                {
                    if (cbNGType.Text.Trim() == "若NG选择")
                    {
                        cbNGType.Focus();
                        MessageBox.Show("请选择异常类型。");
                        return;
                    }
                    productTaskRecordNG.State = 5;
                }
                else if (rbtReDo.Checked)//6品质判定重做
                {
                    productTaskRecordNG.State = 6;
                    if (rbtSongDing.Checked)
                    {
                        nReDoSongDing = 1;
                    }
                    else
                    {
                        nReDoSongDing = 2;
                    }
                }
                else
                {
                    MessageBox.Show("请判定结果。");
                    return;
                }
                if (bIsWorkModel)
                {
                    Int64 nReturn = ProductTaskRecordDbHelper.ProductTaskRecordPZUpdate(productTaskRecordNG);
                    if (nReturn < 1)
                    {
                        MessageBox.Show("Error:行93 ProductTaskRecordPZUpdate生产任务记录更新异常。");
                        return;
                    }
                }
                nCheckState = productTaskRecordNG.State;
                this.Hide();
            }
        }

        private void BtCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

    }
}
